/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */

package com.pig4cloud.ila.client.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.ila.client.api.entity.Article;
import com.pig4cloud.ila.client.service.ArticleService;
import com.pig4cloud.pig.common.core.constant.CommonConstants;
import com.pig4cloud.pig.common.core.constant.enums.ArticleStateEnum;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import com.pig4cloud.pig.common.security.util.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;


/**
 * 文章管理表
 *
 * @author KonBAI
 * @date 2020-10-14 23:12:14
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/article")
@Api(value = "article", tags = "文章管理")
public class ArticleController {

    private final ArticleService articleService;

    /**
     * 分页查询
     *
     * @param page    分页对象
     * @param article 文章管理表
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page")
    public R getArticlePage(Page page, Article article) {
        boolean titleFlag = ObjectUtil.isNotEmpty(article.getTitle());
        boolean authorFlag = ObjectUtil.isNotEmpty(article.getTitle());
        return R.ok(articleService.page(page, Wrappers.lambdaQuery(new Article())
                .eq(Article::getDelFlag, CommonConstants.STATUS_NORMAL)
                .eq(Article::getStateFlag, ArticleStateEnum.PASS.getCode())
                .eq(Article::getCreateBy, SecurityUtils.getMemberId())
                .and(titleFlag && authorFlag, i -> i
                        .like(titleFlag, Article::getTitle, article.getTitle())
                        .or()
                        .like(authorFlag, Article::getAuthor, article.getAuthor()))
                .orderByDesc(Article::getCreateTime)));
    }


    /**
     * 通过id查询文章管理表
     *
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}")
    public R getById(@PathVariable("id") String id) {
        return R.ok(articleService.getById(id));
    }

    /**
     * 新增文章管理表
     *
     * @param article 文章管理表
     * @return R
     */
    @ApiOperation(value = "新增文章管理表", notes = "新增文章管理表")
    @SysLog("新增文章管理表")
    @PostMapping
    // @PreAuthorize("@pms.hasPermission('creation_article_add')")
    public R save(@RequestBody Article article) {
        article.setAuthor(SecurityUtils.getMember().getUsername());
        return R.ok(articleService.save(article));
    }

    /**
     * 修改文章管理表
     *
     * @param article 文章管理表
     * @return R
     */
    @ApiOperation(value = "修改文章管理表", notes = "修改文章管理表")
    @SysLog("修改文章管理表")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('creation_article_edit')")
    public R updateById(@RequestBody Article article) {
        return R.ok(articleService.updateById(article));
    }

    /**
     * 通过id删除文章管理表
     *
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除文章管理表", notes = "通过id删除文章管理表")
    @SysLog("通过id删除文章管理表")
    @DeleteMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('creation_article_del')")
    public R removeById(@PathVariable String id) {
        return R.ok(articleService.removeById(id));
    }

}
